const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    basicsList: [{
      icon: 'roundcheckfill',
      color: 'green',
      name: '已完成'
    }, {
      icon: 'usefullfill',
      color: 'blue',
      name: '进行中'
    }, {
      icon: 'radioboxfill',
      color: 'gray',
      name: '未完成'
    }, ],
    basics: 0,
    modalName: "",
    name: null,
    bedNum: null,
    hospNum: null,
    desc: null,
    btnText: "确定",
    flag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var self = this
    let flowID = options.id
    //测试
    // let flowID = '7'
    // 页面初始化 -> 获取flow所有信息
    this.setData({
      flowID: flowID,
      height: wx.getSystemInfoSync().windowHeight
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 确保返回该页面的时候刷新数据
    this.getFlow()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getFlow: function() {
    var self = this
    var url = Api.getFlow + '/' + self.data.flowID
    var params = {
      flowId: self.data.flowID
    };
    var method = 'GET';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      console.log("getFlow res", res)
      let currentID = self.getCurrentItemID(data.flowStepList)
      self.setData({
        flowInfo: data,
        flowStepList: data.flowStepList,
        name: data.flowPatientName,
        bedNum: data.bedNum,
        hospNum: data.hospNum,
        desc: data.description,
        currentID: currentID
      })
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  // 获取“进行中”的id
  getCurrentItemID(flowStepList) {
    let res = null
    for (var i = 0; i < flowStepList.length; i++) {
      if (flowStepList[i].state == 1) {
        res = 'currentID_' + flowStepList[i].stepId + '_1'
        break;
      }
    }
    if (res == null) {
      res = 'currentID_' + flowStepList[flowStepList.length - 1].stepId + '_2'
    }
    return res
  },
  fail: function(e) {
    console.log("请求失败：", e)
  },
  scrollSteps() {
    this.setData({
      scroll: this.data.scroll == 5 ? 0 : this.data.scroll + 1
    })
  },
  /**
   * 操作选择(查看病人信息|查看医生信息|查看问卷|填写问卷)
   */
  selcetOptions: function(e) {
    var self = this
    let type = e.currentTarget.dataset.type
    let state = e.currentTarget.dataset.state
    let flowStepId = e.currentTarget.dataset.flowstepid
    console.log("type: ", type)
    console.log("state: ", state)
    console.log("flowStepId: ", flowStepId)

    if (state == 0) {
      wx.showToast({
        icon: 'none',
        title: '请先完成上一步',
      })
    } else {
      if (type > 0) { //查看患者问卷
        if (state == 1) {
          wx.showToast({
            icon: 'loading',
            title: '待完成',
          })
          return
        }
        self.toSurvey(type, state, flowStepId)
      } else {
        if (type == -1) { //查看患者信息
          self.getPersonInfo()
        } else { //查看医师信息
          self.getDoctorInfo()
        }
      }
    }
  },
  hideInfo: function(e) {
    let flag = e.currentTarget.dataset.target
    if (flag) {
      // 修改资料
      this.updatePatient()
    } else {
      this.setData({
        modalName: null
      })
    }
  },
  getPersonInfo: function() {
    let self = this
    self.setData({
      modalName: 'personInfo'
    })
  },
  getDoctorInfo: function() {
    var self = this
    let flowStepList = self.data.flowStepList
    wx.navigateTo({
      url: '../doctorInfo/doctorInfo?flowID=' + self.data.flowInfo.id + '&doctorId=' + self.data.flowInfo.doctorId + '&flowStepLength=' + flowStepList.length + '&templeteId=' + self.data.flowInfo.templeteId,
    })
  },
  toSurvey: function(type, state, flowStepId) {
    // console.log("type: ", type)
    // console.log("state: ", state)

    wx.navigateTo({
      url: '../../survey/survey?state=' + state + '&type=' + type + '&flowStepId=' + flowStepId + '&role=doctor',
    })
  },
  getName: function(e) {
    this.setData({
      name: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  getBedNum: function(e) {
    this.setData({
      bedNum: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  getHospNum: function(e) {
    this.setData({
      hospNum: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  getDesc: function(e) {
    this.setData({
      desc: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  updatePatient: function() {
    var self = this
    console.log("修改患者资料")
    var url = Api.flow_updata_patient
    var params = {
      flowId: self.data.flowInfo.id,
      desc: self.data.desc,
      bedNum: self.data.bedNum,
      hospNum: self.data.hospNum,
      flowPatientName: self.data.name,
    };
    var method = 'POST';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      app.globalData.loginUser = data
      if (res.code == 200) {
        wx.showToast({
          title: '修改成功',
        })
        console.log("修改信息成功res: ", res)
        self.setData({
          modalName: null,
          flag: false,
          btnText: "确定",
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: '修改失败'
        })
      }
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  fail: function(e) {
    console.log("请求失败：", e)
  },
  nextStep: function() {
    let flowStepList = this.data.flowStepList
    if(flowStepList.length == 2 && flowStepList[1].state == 1) {
      wx.showToast({
        title: '请先选定流程！',
        icon: 'error',
        duration: 2000
      })
      return
    }
    var self = this;
    let flowID = self.data.flowInfo.id
    wx.showModal({
      title: '提示',
      content: '确认要下一步吗？',
      success(res) {
        if (res.confirm) {
          var url = Api.push_flow + '/' + flowID
          var params = {

          };
          var method = 'POST';
          var header = "application/json";
          var success = function(res) {
            let data = res.data
            if (res.code == 200) {
              console.log("下一步成功：", res)
              self.setData({
                flowStepList: data
              })
            } else {
              wx.showModal({
                title: '提示',
                content: res.msg,
              })
            }

          }
          ApiRequest.request(url, params, method, header, success, self.fail);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 跳转到聊天反馈界面
   */
  toChat: function() {
    var flowID = this.data.flowID
    wx.navigateTo({
      url: '../../chat/chat?flowID=' + flowID,
    })
  },
   /**
   * 刷新flow信息
   */
  refreshFlow: function() {
    this.getFlow()
    wx.showToast({
      title: '刷新成功',
    })
  }
})